package com.fingard.dsp.bank.directbank.ynht01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/3/12.
 */
public class YNHT01SinglePay extends YNHT01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            YNHT01Header header = new YNHT01Header();
            header.tranCode ="510001";
            Date date = new Date();
            header.termDate = Format.Date8Format.format(date);
            header.termTime = Format.Time6Format.format(date);
            header.seqNo = p_req1908.singleDetail.askNumber;
            writeCommonHeader(xmlTextWriter,header);
            XElement req_param = new XElement("ReqParam","");

//            TfrType	转账方式	1	必填	0�C行内，1�C跨行
            req_param.addChild(new XElement("TfrType","1".equals(p_req1908.singleDetail.sameBnk)?"0":"1"));
//            AcctNo	帐号	32	必填
            req_param.addChild(new XElement("AcctNo",p_req1908.singleDetail.payAct));
//            AcctName	转出户名	70	必填
            req_param.addChild(new XElement("AcctName",p_req1908.singleDetail.payName));
//            AcctNo1	转入账号	32	必填
            req_param.addChild(new XElement("AcctNo1",p_req1908.singleDetail.recAct));
//            	转入户名	70	必填
            req_param.addChild(new XElement("AcctName1",p_req1908.singleDetail.recName));
//            BranchNo1	转入帐号开户行号	20	必填	行内填空
            req_param.addChild(new XElement("BranchNo1",p_req1908.singleDetail.cnapsCode));
//            BranchName1	转入帐号开户行名	120	必填	行内填空
            req_param.addChild(new XElement("BranchName1",p_req1908.singleDetail.cnapsName));
//            Ccy	币种	3	必填	人民币填“CNY”，目前只支持人民币，填其他币种也转为人民币处理
            req_param.addChild(new XElement("Ccy",convertCurToBank(p_req1908.singleDetail.recCur)));
//            Amount	转账金额	17,2	必填
            req_param.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
//            CustType	转入账户类型	1	必填	0-对私，1-对公
            req_param.addChild(new XElement("CustType","1".equals(p_req1908.singleDetail.isPrivate)?"0":"1"));
//            CertNo	凭证号	32	必填
            req_param.addChild(new XElement("CertNo",p_req1908.singleDetail.abstractATS));//此处存入对账码
//            Memo	备注	255	必填
            req_param.addChild(new XElement("Memo",p_req1908.singleDetail.purpose));//此处备注改成去用途。

            nodeAnalysis(p_req1908.singleDetail.srcXElement,req_param,getCharset());
            xmlTextWriter.writeXElement(req_param);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspCode");
                    String returnMsg = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspMsg");
                    detail.respCode = "0";
                    detail.respInfo = returnMsg;
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;//查证使用第三方流水号
                    detail.payInfoCode = returnCode;
                    detail.transState = returnCode;
                    detail.outTransID = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/SerSeqNo");
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            } else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            YNHT01Header header = new YNHT01Header();
            header.tranCode ="510002";
            Date date = new Date();
            header.termDate = Format.Date8Format.format(date);
            header.termTime = Format.Time6Format.format(date);
            header.seqNo = p_req1944.reqHeader.transSeqID;
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("ReqParam");
//            AcctNo	转出帐号	32	必填
            xmlTextWriter.writeElementString("AcctNo",p_req1944.payAct);
//            OldTermDate	原第三方交易日期	8	必填
            xmlTextWriter.writeElementString("OldTermDate",p_req1944.bgnDate.substring(0,10).replace("-",""));
//            OldSeqNo	原第三方流水号	8	必填
            xmlTextWriter.writeElementString("OldSeqNo",p_req1944.askNumber);
            xmlTextWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspCode");
                        String returnMsg = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspMsg");
                        if ("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/HTBankData/RspData/Result/TranStatusDesc");
                            detail.reqSeqID = p_req1944.askNumber;
                            detail.transState= tmpBkReader.getFirstText("/HTBankData/RspData/Result/TranStatus");
                            detail.payInfoCode = detail.transState;
                        }else {
                                detail.respCode = "-1";
                                detail.respInfo = returnCode+" : "+returnMsg;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
